package com.lecture.springlecture.controller;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.model.DemoData;
import com.lecture.springlecture.entity.Menu;
import com.lecture.springlecture.service.MenuService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2020/8/6 14:16
 */
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("getMenuList")
    public List<Menu> getMenuList() {
        return menuService.getMenuList();
    }


    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
        List<DemoData> list = new ArrayList<>();
        DemoData demoData = new DemoData();
        demoData.setId(1L);
        demoData.setString("a");
        demoData.setDate(new Date());
        demoData.setStr("se2343t");
        demoData.setDoubleData(132.7);

        DemoData demoData1 = new DemoData();
        demoData1.setId(1L);
        demoData1.setString("a");
        demoData1.setDate(new Date());
        demoData1.setStr("s34et");
        demoData1.setDoubleData(12.237);

        DemoData demoData2 = new DemoData();
        demoData2.setId(1L);
        demoData2.setString("a");
        demoData2.setDate(new Date());
        demoData2.setStr("set34");
        demoData2.setDoubleData(12223.7);

        list.add(demoData);
        list.add(demoData1);
        list.add(demoData2);

        EasyExcel.write(response.getOutputStream(), DemoData.class).sheet().doWrite(list);
    }

    @GetMapping("export")
    public void export(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
            int[] mergeColumnIndex = {0};
            // 从哪一行开始合并
            int mergeRowIndex = 1;
            /*EasyExcelFactory.write(response.getOutputStream())
                    .sheet(1)
                    .head(getHead())
                    .registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumnIndex))
                    .doWrite(contentData());*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static List<List<String>> getHead() {
        List<List<String>> headTitles = Lists.newArrayList();
        String deviceType = "设备分类", nodeName = "所属节点", tagCode = "测点编码";
        headTitles.add(Lists.newArrayList(deviceType,deviceType,deviceType));
        headTitles.add(Lists.newArrayList(nodeName,nodeName,nodeName));
        headTitles.add(Lists.newArrayList(tagCode,tagCode,tagCode));

        List<String> dutyList = getDuty();
        dutyList.forEach(duty -> {
            headTitles.add(Lists.newArrayList(duty,"上限","次数"));
            headTitles.add(Lists.newArrayList(duty,"上限","最高温"));
            headTitles.add(Lists.newArrayList(duty,"下限","次数"));
            headTitles.add(Lists.newArrayList(duty,"下限","最高温"));
        });
        return headTitles;
    }

    public static List<String> getDuty() {
        return Lists.newArrayList("一值","二值","三值","四值","五值");
    }

    private static List<List<Object>> contentData(){
        List<List<Object>> contentList = Lists.newArrayList();
        //这里一个List<Object>才代表一行数据，需要映射成每行数据填充，横向填充（把实体数据的字段设置成一个List<Object>）
        contentList.add( Lists.newArrayList("测试", "商品A","苹果🍎","","k") );
        contentList.add( Lists.newArrayList("测试", "商品B","橙子🍊") );
        return contentList;
    }
}
